package com.javaweb.service.impl;

import com.javaweb.aop.GranularType;
import com.javaweb.aop.TimeMeasure;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import com.javaweb.service.UserService;
import com.javaweb.service.dto.PageResult;
import com.javaweb.service.dto.UserConditionDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: hjm
 * @date: 2024/9/23 12:36
 */
@TimeMeasure(granular = GranularType.MIL_SECOND)
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao ;
    @Override
    public User login(String username, String password) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectOne(username, password) ;
    }

    @Override
    public List<User> findUserByName(String username) {
        User user = new User() ;
        if( user.getName().equals( "123")) {

        }
        return userDao.selectByName(username) ;
    }

    @Override
    public User queryByName(String username){
        User user = new User() ;
        user=userDao.selectByName2(username);
        return user;

    }

    @Override
    @Transactional
    public int register(User user) {
        try {
            // 尝试插入用户到数据库
            int result = userDao.insert(user);

            // 检查插入操作是否成功
            if (result > 0) {
                log.info("用户注册成功，用户ID：{}", user.getUserId());
                return 1; // 注册成功
            } else {
                log.warn("用户注册失败，用户ID：{}", user.getUserId());
                return 0; // 注册失败
            }
        } catch (Exception e) {
            // 记录异常信息到日志
            log.error("注册用户失败，用户信息：{}", user, e);
            throw new RuntimeException("注册用户失败", e);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult queryUsers(User userCondition, Integer pageNum, Integer pageSize) {
        // 计算开始的记录数
        int start = (pageNum - 1) * pageSize;
        // 查询总记录数
        int totalItems = userDao.selectCountAll();
        // 查询当前页的用户列表
        List<User> users = userDao.selectByName(userCondition.getName());
        // 计算总页数
        int totalPages = (int) Math.ceil((double) totalItems / pageSize);

        PageResult<User> pageResult = new PageResult<>();
        pageResult.setData(users);
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotal(totalItems);

        return pageResult;
    }

    @Override
    @Transactional
    public void addAssignRole(String userId, List<String> roleIds) {
        try {
            for (String roleId : roleIds) {
                userDao.insertUserRole(Integer.parseInt(userId), Integer.parseInt(roleId));
            }
        } catch (Exception e) {
            // 进行日志记录等操作
            log.error("分配角色失败，用户ID：{}, 角色IDs：{}", userId, roleIds, e);
            throw new RuntimeException("分配角色失败", e);
        }
    }


}
